/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }
 */
/**
 * @param {TreeNode} root
 * @return {boolean}
 */
var isSymmetric = function (root) {
  if (!root) return true;
  let result = true;
  function check(l, r) {
    if (!result) return;
    if (!l && !r) return;
    if (!l || !r) {
      result = false;
      return;
    }
    if (l.val != r.val) {
      result = false;
      return;
    }

    check(l.left, r.right);
    check(r.left, l.right);
  }

  check(root.left, root.right);
  return result;
};
